/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.tanesha.recaptcha.ReCaptchaResponse;
import net.tanesha.recaptcha.ReCaptchaImpl;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Don
 */
public class ServletLogIn extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Cookie userCookie;
            Hashtable cookies = new Cookies().cookieTable(request.getCookies());
        
        
        
        String username;
            String password;
       int count;
        long creationTime;
        long lastAccessTime;
        long interval;
        HttpSession session = request.getSession(true);
        count = (Integer)session.getAttribute("count");
            username = request.getParameter("username");
            password = request.getParameter("password");

            if(username != null)
            {
                if(username.equals(""))
                {
                    userCookie = new Cookie("name", "");
                    userCookie.setMaxAge(0);
                    username = null;
                }
                else {
                userCookie = new Cookie("name", username);
                userCookie.setMaxAge(3600);
                }            
                response.addCookie(userCookie);
            }
             if(count < 3)
        {
            Customer cs;
            cs  = login(username,password,session);
            if(cs != null)
            {     
                session.setAttribute("username", cs.getUsername());
                session.setAttribute("login", true);
                switch(cs.getUserType())
                {
                    case 0:
                        session.setAttribute("userType", 0);
                        session.setAttribute("count", 0);
                        response.sendRedirect("user_home.jsp");
                        break;
                    case 1:
                        session.setAttribute("userType", 1);
                        session.setAttribute("count", 0);
                        response.sendRedirect("admin_page.jsp");
                        break;
                    case 2:
                        session.setAttribute("userType", 2);
                        session.setAttribute("count", 0);
                        response.sendRedirect("manager_page.jsp");
                        break;
                }
            }                
            else
            {
                creationTime = session.getCreationTime();
                lastAccessTime = session.getLastAccessedTime();
                interval = lastAccessTime - creationTime;
                if(interval > 1000)
                {   
                    count +=1;
                    session.setAttribute("count", count);                   
                }
                
                response.sendRedirect("generic_login.jsp");
            }               
        }
        System.out.println(count);
        if(count >= 3)
        {        
            String remoteAddr = request.getRemoteAddr();
            ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
            reCaptcha.setPrivateKey("6LfdCc4SAAAAAI4p64kJ91fJl4NHEFMULd1ZP4QS");

            String challenge = request.getParameter("recaptcha_challenge_field");
            String uresponse = request.getParameter("recaptcha_response_field");
            ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);
            if (reCaptchaResponse.isValid()) 
            {
                Customer cs;
                cs  = login(username,password,session);
                if(cs != null)
                {     
                    session.setAttribute("login", true);
                    switch(cs.getUserType())
                    {
                        case 0:
                            session.setAttribute("userType", 0);
                            response.sendRedirect("user_home.jsp");
                            break;
                        case 1:
                            session.setAttribute("userType", 1);
                            response.sendRedirect("admin_page.jsp");
                            break;
                        case 2:
                            session.setAttribute("userType", 2);
                            response.sendRedirect("manager_page.jsp");
                            break;
                    }
                }                
                else
                {                  
                    creationTime = session.getCreationTime();
                    lastAccessTime = session.getLastAccessedTime();
                    interval = lastAccessTime - creationTime;
                    
                    if(interval > 1000)
                    {   
                        count +=1;
                        session.setAttribute("count", count);                   
                    }
                    response.sendRedirect("generic_login.jsp");
                }
            } 
            else 
            {
                response.sendRedirect("generic_login.jsp");
            }
         }
         
            
       
        
    }
    private Customer login(String username, String password, HttpSession session)
    {
       Customer al;
       al = new CustomerManager().checkCustomer(username, password);
       return al;        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
